<?php

namespace App\Http\Controllers;

use App\App;
use App\Appcat;
use Carbon\Carbon;
use Illuminate\Http\Request;

class AppController extends Controller
{
    //软件列表api
    public function applist($catid)
    {
        $data['cats'] = Appcat::orderBy('sort', 'desc')->select('id', 'catname')->get();
        if ($catid) {
            $apps = App::where('appcatid', $catid)->orderBy('id', 'desc')->get();
        } else {
            $apps = App::where('appcatid', $data['cats'][0]['id'])->orderBy('id', 'desc')->get();
        }
        $data['apps'] = new \App\Http\Resources\AppCollection($apps);
        return $data;
    }

    //案例详情api
    public function appinfo($id)
    {
        $info = App::findOrfail($id);
        return new \App\Http\Resources\App($info);
    }

    //时间处理api
    public function viptime($time)
    {
        $userinfo = request()->user();
        if($userinfo->is_vip) {
            $time = Carbon::parse($userinfo->vipduetime)->addmonth($time)->format('Y年m月d日');
        }else{
            $time = Carbon::parse('+' . $time . 'months')->format('Y年m月d日');
        }
        return $time;
    }
}
